<?php
if (!isset($CfgPara)) {
    require_once("{$_SERVER['DOCUMENT_ROOT']}/class/cfg.class.php");
    $CfgPara = new cfg();
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head lang="zh-cn">
    <meta charset="utf8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Sneaker Custom</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <meta property="wb:webmaster" content="def3cb996b1dc007"/>
    <link rel="stylesheet" type="text/css" href="<?= $CfgPara->httproot ?>/css/amazeui.min.css"/>
    <link rel="stylesheet" type="text/css" href="<?= $CfgPara->httproot ?>/css/app.css"/>
    <script type="text/javascript" src="<?= $CfgPara->httproot ?>/js/jquery.min.js"></script>
    <script type="text/javascript">
        function set_user_name() {
            var user_name = $("#set_user_weibo_name").val();
            if (typeof (user_name) == "undefined") return;
            if (user_name == '') {
                $("#user_weibo_warning").text('用户名不能为空');
            }
            $.ajax({
                url: '/index.php',
                type: 'POST',
                data: {
                    controller: 'user_login',
                    user_weibo_name: user_name
                },
                timeout: 5000,
                error: function () {
                    $("#user_weibo_warning").text('无法连接服务器！');
                },
                success: function (result) {
                    if (result == 1)
                        location.assign('http://www.sneaker-custom.com');
                    else
                        $("#user_weibo_warning").text(result);
                }
            });
        }
    </script>
</head>
<body>
<div id="page-header" class="app-main-backcolor"
     style="position:fixed;top:0px;left:0px;height:40px;width:100%;z-index:1;">
    <div class="am-fl">
        <h2 style="margin:0px;height:40px;width:180px;padding-top:3px;padding-left:5px">
            <a href="<?= $CfgPara->httproot ?>" class="app-main-backcolor">
                <i class="am-header-icon am-icon-angle-left"></i>
            </a>
            <span>Sneaker&nbsp;Custom</span>
        </h2>
    </div>
</div>
<div style="height:40px"></div>
<div style="padding:15px">
    <?php
    session_start();

    require_once("{$CfgPara->root}/sina_weibo/config.php");
    require_once("{$CfgPara->root}/sina_weibo/saetv2.ex.class.php");

    $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);

    if (isset($_REQUEST['code'])) {
        $keys = array();
        $keys['code'] = $_REQUEST['code'];
        $keys['redirect_uri'] = WB_CALLBACK_URL;
        try {
            $token = $o->getAccessToken('code', $keys);
        } catch (OAuthException $e) {
        }
    }
    if (isset($token)) {
        $_SESSION['weibo_token'] = $token;
        setcookie('weibojs_' . $o->client_id, http_build_query($token));
        $c = new SaeTClientV2(WB_AKEY, WB_SKEY, $_SESSION['weibo_token']['access_token']);
        $user_weibo_message = $c->show_user_by_id($_SESSION['weibo_token']['uid']);
        //print_r($user_weibo_message);
    }
    if (isset($user_weibo_message['id'])) {
        require("{$CfgPara->root}/controller/user_login.c.php");
    } else {
        ?>
        <div class="am-radius am-alert am-alert-warning">
            <div class="am-text-lg">新浪微博授权失败</div>
            <p><a href="<?= $CfgPara->httproot ?>" class="am-radius am-btn am-btn-default am-btn-sm app-black-color">点击返回</a>
            </p>
        </div>
        <?php
    }
    ?>
    <span id="user_weibo_warning" class="am-badge am-badge-danger"></span>
</div>
</body>
</html>